# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=nh3
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.2.21
pkgrel=1
pkgdesc='Ammonia HTML sanitizer Python binding (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://nh3.readthedocs.io/'
msys2_repository_url='https://github.com/messense/nh3'
msys2_references=(
  'purl: pkg:pypi/nh3'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-maturin")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('4990e7ee6a55490dbf00d61a6f476c9a3258e31e711e13713b2ea7d6616f670e')

prepare() {
  cd "${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cargo fetch --locked
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  export WINAPI_NO_BUNDLED_LIBRARIES=1
  export _PYTHON_HOST_PLATFORM=$(python -c "import sysconfig, sys; sys.stdout.write(sysconfig.get_platform())")
  "${MINGW_PREFIX}"/bin/maturin build --release --strip --locked --all-features
}

package() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    "${MINGW_PREFIX}"/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" target/wheels/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
